<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 支付宝同步接收控制器
 * @package  onlinepay
 * @author   allen.qiang
 * @version 1.0
 */

require('core/shop.php');
quanli_require('module/base/system/sysPayment.class.php');
quanli_include('module/base/common/quanliOnlinepay.class.php');


//实例支付类
$ebsigOnlinepay = new quanliOnlinepay();

//实例支付方式类
$sysPayment = new sysPayment();

//实例seo类
$seo = new seo();


//接收返回参数
$ebsigOnlinepay->receiveParameter();


//会员号、支付方式ID、订单类型
list($pcustID, $payTypeID, $billType) = explode('|', $ebsigOnlinepay->getParameter('extra_common_param'));

//订单号
$bill_no = $ebsigOnlinepay->getParameter('out_trade_no');

//查询支付方式参数
$online_parameter_array = $sysPayment->searchOnlinepPrameter( $payTypeID );

$shop_array = array();

//检查签名和支付状态
$sign_str = $ebsigOnlinepay->createSignStr(array('sign', 'subsite', 'sign_type'));
$sign_str .= $online_parameter_array['md5_key'];
if (md5($sign_str) != $ebsigOnlinepay->getParameter('sign') || !in_array($ebsigOnlinepay->getParameter('trade_status'), array('TRADE_FINISHED', 'TRADE_SUCCESS'))) {
    $shop_array['success'] = 0;
} else {
    $shop_array['success'] = 1;
    $shop_array['total_amount'] = $ebsigOnlinepay->getParameter('total_fee');
}

//保存客户端返回数据
$ebsigOnlinepay->setReceiveParams( $bill_no, 2);


//保存服务端返回数据
$result_code = $ebsigOnlinepay->setReceiveParams($bill_no, 1, 3);
/////////////////////////////////////////////////////////////////
//$result_code 1：订单在本次支付前未付款 -1：订单在本次支付前已付款
if ($result_code == 1) {
    $arg_array = array(
        'bill_no' => $bill_no,
        'pcustID' => $pcustID,
        'payTypeID' => $payTypeID,
        'billType' => $billType,
        'total_amount' => $ebsigOnlinepay->getParameter('total_fee'),
        'pay_code' => $ebsigOnlinepay->getParameter('trade_no')
    );
    $ebsigOnlinepay->payAction( $arg_array );
}
/////////////////////////////////////////////////////////////////
//生成seo信息
$shop_array['seo'] = $seo->shopSeo('订单支付成功页');

//页面值
$shop_array['page_name'] = 'payment_online_success';

$G_SHOP->smarty->assign_by_ref('shop_array', $shop_array);

$G_SHOP->smarty->display('payment_online_close.tpl');